package com.shinbada.modules.sys.web;

import com.shinbada.common.json.AjaxJson;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.modules.sys.entity.Log;
import com.shinbada.modules.sys.service.LogService;
import com.shinbada.modules.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 日志Controller
 *
 * @author initcap
 * @version 2016-6-2
 */
@RestController
@RequestMapping("/sys/log")
@Api(tags = "日志管理")
public class LogController extends BaseController {

    @Autowired
    private LogService logService;

    @RequiresPermissions("user")
    @GetMapping("list")
    @ApiOperation("分页获取日志")
    public AjaxJson data(Log log, HttpServletRequest request, HttpServletResponse response) {
        Page<Log> page = logService.findPage(new Page<>(request, response), log);
        return AjaxJson.success().put("page", page);
    }

    @RequiresPermissions("user")
    @GetMapping("data/mine")
    @ApiOperation("获取自己相关的日志")
    public AjaxJson mine(Log log, HttpServletRequest request, HttpServletResponse response) {
        log.setCreateBy(UserUtils.getUser());
        Page<Log> page = logService.findPage(new Page<>(request, response), log);
        return AjaxJson.success().put("page", page);
    }

    @RequiresPermissions("sys:log:del")
    @DeleteMapping("delete")
    @ApiOperation("删除日志")
    public AjaxJson deleteAll(String ids) {
        logService.batchDelete(ids.split(","));
        return AjaxJson.success("删除日志成功！");
    }

    @RequiresPermissions("sys:log:del")
    @DeleteMapping("empty")
    @ApiOperation("清空日志")
    public AjaxJson empty() {
        logService.empty();
        return AjaxJson.success("清空日志成功!");
    }
}
